const { Controller } = require('egg');
const validate = require('../rule/rule');

class AddressController extends Controller {
  /**
   *
   * @param req
   * @param res
   * @api { GET } /user/address/list 地址列表
   * @apiName list
   * @apiGroup address
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery { Number } pageIndex 页码
   * @apiQuery { Number } pageSize 页面大小
   * @apiQuery { String } order 排序字段
   * @apiQuery { String } method 排序方式
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async list(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { pageIndex, pageSize, order, method } = validate.addressInsertValidate(query);
      const data = ctx.service.address.list(pageIndex, pageSize, order, method);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/address/insert 新增地址
   * @apiName insert
   * @apiGroup address
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { Number } pageIndex 页码
   * @apiParam { Number } pageSize 页面大小
   * @apiParam { String } order 排序字段
   * @apiParam { String } method 排序方式
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async insert(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { address, user_email, user_phone, user_name } = await validate.addressInsertValidate(body);
      const data = ctx.service.address.insert(address, user_email, user_phone, user_name);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/address/update 更新地址
   * @apiName update
   * @apiGroup address
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } address 地址
   * @apiParam { String } user_email 邮箱
   * @apiParam { String } user_phone 电话号码
   * @apiParam { String } user_name 名称
   * @apiParam { String } uuid 地址id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async update(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { address, user_email, user_phone, user_name, uuid } = validate.addressUpdateValidate(body);
      const data = ctx.service.address.update(address, user_email, user_phone, user_name, uuid);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /user/address/delete 删除地址
   * @apiName delete
   * @apiGroup address
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { Object } ids 地址ids
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */
  async delete(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { ids } = validate.addressDeleteValidate(query);
      const data = ctx.service.address.delete(ids);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = AddressController;
